home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
PROGRAMR
/
OLE2BOOK.ZIP
/
CHAP05.ZIP
/
CHAP05
/
SCHMOO
/
DOCUMENT.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-13
|
19KB
|
901 lines
/*
* DOCUMENT.CPP
*
* Implementation of the CSchmooDoc derivation of CDocument as
* well as an implementation of CPolylineAdviseSink.
*
* Copyright (c)1993 Microsoft Corporation, All Rights Reserved
*
* Kraig Brockschmidt, Software Design Engineer
* Microsoft Systems Developer Relations
*
* Internet : kraigb@microsoft.com
* Compuserve: >INTERNET:kraigb@microsoft.com
*/
#include "schmoo.h"
/*
* CSchmooDoc::CSchmooDoc
* CSchmooDoc::~CSchmooDoc
*
* Constructor Parameters:
* hInst HINSTANCE of the application.
*/
CSchmooDoc::CSchmooDoc(HINSTANCE hInst)
: CDocument(hInst)
{
m_pPL=NULL;
m_pPLAdv=NULL;
m_uPrevSize=SIZE_RESTORED;
return;
}
CSchmooDoc::~CSchmooDoc(void)
{
//Clean up the allocations we did in FInit
if (NULL!=m_pPL)
delete m_pPL;
if (NULL!=m_pPLAdv)
delete m_pPLAdv;
return;
}
/*
* CSchmooDoc::FInit
*
* Purpose:
* Initializes an already created document window. The client actually
* creates the window for us, then passes that here for further
* initialization.
*
* Parameters:
* pDI LPDOCUMENTINIT containing initialization parameters.
*
* Return Value:
* BOOL TRUE if the function succeeded, FALSE otherwise.
*/
BOOL CSchmooDoc::FInit(LPDOCUMENTINIT pDI)
{
RECT rc;
//Change the stringtable range to our customization.
pDI->idsMin=IDS_DOCUMENTMIN;
pDI->idsMax=IDS_DOCUMENTMAX;
//Do default initialization
if (!CDocument::FInit(pDI))
return FALSE;
//Add the Polyline stuff we need.
m_pPLAdv=new CPolylineAdviseSink((LPVOID)this);
m_pPL =new CPolyline(m_hInst);
//Attempt to create our contained Polyline.
GetClientRect(m_hWnd, &rc);
InflateRect(&rc, -8, -8);
if (!m_pPL->FInit(m_hWnd, &rc, WS_CHILD | WS_VISIBLE
, ID_POLYLINE, m_pPLAdv))
return FALSE;
return TRUE;
}
/*
* CSchmooDoc::FMessageHook
*
* Purpose:
* Processes WM_SIZE for the document so we can resize the Polyline.
*
* Parameters:
* <WndProc Parameters>
* pLRes LRESULT FAR * in which to store the return value
* for the message.
*
* Return Value:
* BOOL TRUE to prevent further processing, FALSE otherwise.
*/
BOOL CSchmooDoc::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
, LPARAM lParam, LRESULT FAR *pLRes)
{
UINT dx, dy;
RECT rc;
if (WM_SIZE==iMsg)
{
//Don't effect the Polyline size to or from minimized state.
if (SIZE_MINIMIZED!=wParam && SIZE_MINIMIZED !=m_uPrevSize)
{
//When we change size, resize any Polyline we hold.
dx=LOWORD(lParam);
dy=HIWORD(lParam);
/*
* If we are getting WM_SIZE in response to a Polyline
* notification, then don't resize the Polyline window again.
*/
if (!m_fNoSize && NULL!=m_pPL)
{
//Resize the polyline to fit the new client
SetRect(&rc, 8, 8, dx-8, dy-8);
m_pPL->RectSet(&rc, FALSE);
/*
* We consider sizing something that makes the file dirty,
* but not until we've finished the create process, which
* is why we set fNoDirty to FALSE in WM_CREATE since we
* get a WM_SIZE on the first creation.
*/
if (!m_fNoDirty)
FDirtySet(TRUE);
SetRect(&rc, 0, 0, dx, dy);
if (NULL!=m_pAdv)
m_pAdv->OnSizeChange((LPCDocument)this, &rc);
m_fNoDirty=FALSE;
}
}
m_uPrevSize=wParam;
}
/*
* We return FALSE even on WM_SIZE so we can let the default procedure
* handle maximized MDI child windows appropriately.
*/
return FALSE;
}
/*
* CSchmooDoc::Clear
*
* Purpose:
* Sets all contents in the document back to defaults with no filename.
*
* Paramters:
* None
*
* Return Value:
* None
*/
void CSchmooDoc::Clear(void)
{
//Completely reset the polyline
m_pPL->New();
CDocument::Clear();
m_lVer=0;
return;
}
/*
* CSchmooDoc::ULoad
*
* Purpose:
* Loads a given document without any user interface overwriting the
* previous contents of the Polyline window. We do this by opening
* the file and telling the Polyline to load itself from that file.
*
* Parameters:
* fChangeFile BOOL indicating if we're to update the window title
* and the filename from using this file.
* pszFile LPSTR to the filename to load, NULL if the file is
* new and untitled.
*
* Return Value:
* UINT An error value from DOCERR_*
*/
UINT CSchmooDoc::ULoad(BOOL fChangeFile, LPSTR pszFile)
{
//CHAPTER5MOD
HRESULT hr;
LPSTORAGE pIStorage;
//End CHAPTER5MOD
if (NULL==pszFile)
{
//For a new untitled document, just rename ourselved.
Rename(NULL);
m_lVer=VERSIONCURRENT;
return DOCERR_NONE;
}
//CHAPTER5MOD
/*
* If this is not a Compound File, open the file using STGM_CONVERT
* in TRANSACTED mode to effectively see old files as a storage with
* one stream called "CONTENTS" (which is conveniently the name we use
* in the new files). We must use STGM_TRANSACTED here or else
* the old file will be immediately converted on disk: we only want
* a converted image in memory from which to read. In addition,
* note that we need STGM_READWRITE as well since conversion is
* inherently a write operation.
*/
pIStorage=NULL;
if (NOERROR!=StgIsStorageFile(pszFile))
{
hr=StgCreateDocfile(pszFile, STGM_TRANSACTED | STGM_READWRITE
| STGM_CONVERT | STGM_SHARE_EXCLUSIVE, 0, &pIStorage);
if (FAILED(hr))
{
//If we were denied write access, try to load the old way
if (STG_E_ACCESSDENIED==GetScode(hr))
m_lVer=m_pPL->ReadFromFile(pszFile);
else
return DOCERR_COULDNOTOPEN;
}
}
else
{
hr=StgOpenStorage(pszFile, NULL, STGM_DIRECT | STGM_READ
| STGM_SHARE_EXCLUSIVE, NULL, 0, &pIStorage);
if (FAILED(hr))
return DOCERR_COULDNOTOPEN;
}
if (NULL!=pIStorage)
{
m_lVer=m_pPL->ReadFromStorage(pIStorage);
pIStorage->Release();
}
//End CHAPTER5MOD
if (POLYLINE_E_READFAILURE==m_lVer)
return DOCERR_READFAILURE;
if (POLYLINE_E_UNSUPPORTEDVERSION==m_lVer)
return DOCERR_UNSUPPORTEDVERSION;
if (fChangeFile)
Rename(pszFile);
//Importing a file makes things dirty
FDirtySet(!fChangeFile);
return DOCERR_NONE;
}
/*
* CSchmooDoc::USave
*
* Purpose:
* Writes the file to a known filename, requiring that the user has
* previously used FileOpen or FileSaveAs in order to have a filename.
*
* Parameters:
* uType UINT indicating the type of file the user requested
* to save in the File Save As dialog.
* pszFile LPSTR under which to save. If NULL, use the current name.
*
* Return Value:
* UINT An error value from DOCERR_*
*/
UINT CSchmooDoc::USave(UINT uType, LPSTR pszFile)
{
LONG lVer, lRet;
UINT uTemp;
BOOL fRename=TRUE;
//CHAPTER5MOD
HRESULT hr;
LPSTORAGE pIStorage;
//End CHAPTER5MOD
if (NULL==pszFile)
{
fRename=FALSE;
pszFile=m_szFile;
}
/*
* Type 1 is the current version, type 2